import 'es6-promise/auto'
import 'element-ui/lib/theme-chalk/index.css'
import App from '@/app.vue'
import Vue from 'vue'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui'
// import _ from 'lodash'
// 库、sdk
import api from '@/api/api'
import router from '@/router/router'
import request from '@/util/request'
import tools from '@/util/tools'

// component
import aTable from './components/a-table'
import aSelect from './components/a-select'
import pagination from './components/pagination'

// vuex
import store from '@/store/index'
import vueFilter from '@/filter/index'
import vueDirective from '@/directive/index'

// mixin
import elementMixin from '@/mixins/elementPublicMixin'
import oPrivate from './../private-config'
import path from '@/router/path_tmp'

// css
import 'nprogress/nprogress.css'
// import '@/assets/iconfont/iconfont.css'

// 传入 vuex
Vue.prototype.$request = request

window.$tools = tools

// window._ = _

Vue.prototype.$api = api

Vue.prototype.publicPath = oPrivate.publicPath
Vue.prototype.getRouterInfoAccordingName = (name) => {
  const info = path.filter(v => v.name === name)[0]
  return {
    name,
    title: info.meta.title,
    path: info.path
  }
}

// 阻止页面记住滚动
// 在 iphone 上使用，会出现左滑卡顿问题
if ('scrollRestoration' in history) {
  history.scrollRestoration = 'manual'
}

// 兼容低版本浏览器(caniuse)，需要引入polyfill
require('es6-promise').polyfill()

// vue 组件/工具
Vue.use(ElementUI)
Vue.use(VueRouter)
// 添加 filter
vueFilter.forEach(v => {
  Vue.filter(v.name, v.func)
})

// 添加 directive
vueDirective.forEach(v => {
  Vue.directive(v.name, v.func)
})
// 使用全局 mixin
Vue.mixin(elementMixin)

Vue.component('aTable', aTable)
Vue.component('aSelect', aSelect)
Vue.component('pagination', pagination)

Vue.config.productionTip = false

/* eslint-disable */
async function checkLogin() {
  const { code, data } = await api.userGetInfo()
  if (code === 0) {
    Vue.prototype.aFirmId = oPrivate.dev.isReturnMain ? data.list[0].id : data.list[0].id
    store.commit('updateLoginStatus', true)
    store.commit('updateUserData', data)
  }
}

let init = true
let $loading = {}

// 导航钩子，每次变动都会检查登录状态
router.beforeEach(async (to, from, next) => {
  $loading = ElementUI.Loading.service({
    lock: true,
    text: '加载页面中...'
  })
  if (!oPrivate.dev.notValidated) {
    if (init) {
      init = null
      // 第一次验证登录状态
      await checkLogin()
      if (!oPrivate.dev.isReturnMain && store.state.loginStatus) {
        return next({ name: 'main', replace: true })
      }
    }
    const PATHNAME = to.name
    const loginStatus = store.state.loginStatus
    // 验证登录状态
    if (loginStatus) {
      // 已登录
      // if (to.meta.mustCheckAuth && !to.meta.auth) {
      //   return router.push({ name: to.meta.redirect })
      // } else if (PATHNAME === 'login') {
      //   return router.replace({ name: 'main' })
      // }
      if (PATHNAME === 'login') {
        return next({ name: 'main' })
      }
    } else {
      if (PATHNAME !== 'login') {
        return next({ name: 'login' })
      }
    }
  }

  document.title = to.meta.title
  next()
})

router.afterEach(() => {
  $loading.close()
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
